package com.example.reference.practice;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

public class SoftReferenceCache<K, V> {

    private final Map<K, SoftReference<V>> cache = new HashMap<>();

    public void put(K key, V value) {
        SoftReference<V> softReference = new SoftReference<>(value);
        cache.put(key, softReference);
    }

    public V get(K key) {
        SoftReference<V> softReference = cache.get(key);
        if (softReference != null) {
            return softReference.get();
        } else {
            return null;
        }
    }

    public void clear() {
        cache.clear();
    }

}
